package com.zbl.zblproject.hrm.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zbl.zblproject.core.entity.Result;
import com.zbl.zblproject.core.utils.ResultUtil;
import com.zbl.zblproject.hrm.entity.HrmEmployee;
import com.zbl.zblproject.hrm.service.HrmEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author youlanqiang
 * @version 1.0
 * @date 2018/10/29
 */
@RestController
@Api(tags = "员工管理接口")
@RequestMapping("/employee")
public class HrmEmployeeController {

    @Autowired
    private HrmEmployeeService hrmEmployeeService;


    @PostMapping("/add")
    @ApiOperation("添加一个员工")
    public Result addEmployee(HrmEmployee employee){
        if(hrmEmployeeService.addEmployee(employee)){
            return ResultUtil.success("添加成功!");
        }
        return ResultUtil.error("添加失败!");
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除一个员工")
    public Result deleteEmployee(@ApiParam("员工ID") @PathVariable int id){
        if(hrmEmployeeService.deleteEmployeById(id)){
            return ResultUtil.success("删除成功!");
        }
        return ResultUtil.error("删除失败!");
    }

    @PostMapping("/update")
    @ApiOperation("更新员工")
    public Result updateEmployee(HrmEmployee employee){
        if(employee.getId() > 0){
            if(hrmEmployeeService.updateEmployeeById(employee)){
                return ResultUtil.success("更新成功!");
            }
        }
        return ResultUtil.error("更新失败!");
    }


    @GetMapping("/page/{current}/{size}/{status}")
    @ApiOperation("分页")
    public Result page(@ApiParam("当前页面") @PathVariable int current,
                       @ApiParam("项数") @PathVariable int size,
                       @ApiParam("员工状态") @PathVariable int status,
                       @ApiParam("员工名称") @RequestParam("name") String name){
        if(current <=0 || size <= 0){
            return ResultUtil.error("分页参数错误!");
        }
        Page<HrmEmployee> page = new Page<>(current, size);
        return ResultUtil.success("成功!", hrmEmployeeService.pageEmployee(page, status, name));
    }


}
